﻿using LogionDemo.Common.Helper;
using LogionDemo.Models.Enums;
using Masuit.Tools.Security;
using Microsoft.EntityFrameworkCore;

namespace LogionDemo.Models
{
	public class RegisterInfoContext : DbContext
    {
        public DbSet<UserInfo> UserInfos { get; set; }
        public DbSet<LoginSavePasswordConfig> LoginSavePasswordConfigs { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source = RegisterInfo.db");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<UserInfo>(entity =>
            {
                entity.ToTable("UserInfos").HasKey(infos => infos.UserInfoId);
                entity.Property(infos => infos.UserName).IsRequired();
                entity.Property(infos => infos.Password).IsRequired();
                entity.Property(infos => infos.PasswordSalt).IsRequired();
                entity.Property(infos => infos.UserRole).IsRequired().HasDefaultValue(UserRole.User);
                entity.Property(infos => infos.RegistrationDate).IsRequired().HasDefaultValueSql("CURRENT_TIMESTAMP");
            });

            modelBuilder.Entity<LoginSavePasswordConfig>(entity =>
            {
                entity.ToTable("LoginSavePasswordConfig").HasKey(config => config.Id);
                entity.Property(config => config.Id).IsRequired().HasDefaultValue(1);
                entity.Property(config => config.IsRemember).IsRequired().HasDefaultValue(false);
                entity.Property(config => config.User).HasDefaultValue(string.Empty);
                entity.Property(config => config.Password).HasDefaultValue(string.Empty);
            });
        }

        public static void InitializeDatabase()
        {
            using (var context = new RegisterInfoContext())
            {
                context.Database.EnsureCreated();
                if (!context.UserInfos.Any())
                {
                    var passwordSalt = CryptoHelper.GenerateSalt();
                    var user = new UserInfo()
                    {
                        UserName = "Admin",
                        Password = $"Admin{passwordSalt}".SHA256(),
                        PasswordSalt = passwordSalt,
                        UserRole = UserRole.Admin,
                    };
                    context.UserInfos.Add(user);
                }
                if (!context.LoginSavePasswordConfigs.Any())
                {
                    var config = new LoginSavePasswordConfig()
                    {
                        IsRemember = false,
                        User = string.Empty,
                        Password = string.Empty,
                    };
                    context.LoginSavePasswordConfigs.Add(config);
                }
                context.SaveChanges();
            }
        }

        public LoginSavePasswordConfig GetLoginSavePasswordConfig()
        {
            return new LoginSavePasswordConfig()
            {
                IsRemember = LoginSavePasswordConfigs.First().IsRemember,
                User = LoginSavePasswordConfigs.First().User,
                Password = LoginSavePasswordConfigs.First().Password
            };
        }

        public void SetLoginSavePasswordConfig(string user, string password, bool isRemember)
        {
            var config = LoginSavePasswordConfigs.First();
            config.IsRemember = isRemember;
            config.User = user;
            config.Password = password;
            SaveChanges();
        }
    }
}
